﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using Cari.IBP.Platform.Sidecar;
using CCcteg.Common;
using Ccteg.Common;
using Ccteg.DB;
using LinqKit;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Cctegaq.Api.Controllers.Api
{
    /// <summary>
    /// 安全会议
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]

    public class SCFController : IbpControllerBase
    {
        private readonly IMapper _mapper;
        private readonly MySqlDbContext _dbContext;


        public SCFController(MySqlDbContext dbContext, IMapper mapper)
        {

            _dbContext = dbContext;
            _mapper = mapper;
        }
        /// <summary>
        /// 安全会议 添加 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        
        public ResponseModel SCFAddOrUpdate([FromBody] Scf_Scf_ViewModel model)
        {
            var response = ResponseModelFactory.CreateInstance;
            try
            {
                using (_dbContext)
                {
                    var entity = _mapper.Map<Scf_Scf_ViewModel, Scf_Scf>(model);
                    var entryEntity = _dbContext.SCF_Scf.Attach(entity);
                    if (entity.Id > 0)
                    {
                        var editentity = _dbContext.SCF_Scf.AsNoTracking().Where(x => x.Id == entity.Id && x.Deleted == false).AsNoTracking().FirstOrDefault();
                        if (editentity == null)
                        {
                            response.SetFailed("无法修改，数据不存在请刷新页面");
                            return response;
                        }
                        entryEntity.State = EntityState.Modified;
                        entryEntity.Property(p => p.CreatedOn).IsModified = false;
                        entryEntity.Property(p => p.CreatedBy).IsModified = false;
                        entryEntity.Property(p => p.CompanyName).IsModified = false;
                        entryEntity.Property(p => p.CompanyNo).IsModified = false;
                        entryEntity.Property(p => p.Deleted).IsModified = false;
                        entity.ModifiedBy = "系统";
                        entity.ModifiedOn = System.DateTime.Now;
                    }
                    else
                    {
                        entryEntity.Property(p => p.ModifiedBy).IsModified = false;
                        entryEntity.Property(p => p.ModifiedOn).IsModified = false;
                        entity.Deleted = false;
                        entity.CreatedBy = "系统";
                        entity.CreatedOn = System.DateTime.Now;
                        entity.CompanyName = " 中国煤科";
                        entity.CompanyFullName = "中国煤科";
                        entity.CompanyNo = "001";

                    }
                    _dbContext.SaveChanges();
                    response.SetSuccess("操作成功");

                }
            }
            catch (Exception ex)
            {
                response.SetFailed("操作失败");
                response.Data = ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 安全会议 删除
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpPost]
      
        public ResponseModel SCFDelete([Required] int id)
        {
            var response = ResponseModelFactory.CreateInstance;
            try
            {
                using (_dbContext)
                {
                    var res = _dbContext.SCF_Scf.Where(x => x.Id == id)
                        .SetField(x => x.Deleted).WithValue(true)
                        .SetField(x => x.ModifiedOn).WithValue(DateTime.Now)
                        .SetField(x => x.ModifiedBy).WithValue("系统")
                        .Update();
                    if (res > 0)
                        response.SetSuccess("删除成功");
                    else
                        response.SetFailed("删除失败");

                }

            }
            catch (Exception ex)
            {
                response.SetFailed("删除失败");
                response.Data = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 安全会议 详情
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet]
        
        public ResponseModel<Scf_Scf_ViewModel> SCFDetail([Required] int id)
        {
            using (_dbContext)
            {
                var response = new ResponseModel<Scf_Scf_ViewModel>();
                var query = _dbContext.SCF_Scf.AsNoTracking().Where(x => x.Id == id && x.Deleted == false).FirstOrDefault();
                var data = _mapper.Map<Scf_Scf, Scf_Scf_ViewModel>(query);
                response.SetData(data);
                return response;
            }

        }

        /// <summary>
        /// 安全会议查询
        /// </summary>
        /// <param name="scfname">会议名称</param>
        /// <param name="scftype">会议类型(名称)</param>
        /// <param name="year">年度</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">显示条数</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [ResourceDeclare(Identity = "scf/scf/list", Name = "安全会议", Operation = Constants.OPERATION_BROWSE)]
        [ResourceDeclare(Identity = "scf/scf/secondlist", Name = "二级单位会议查询", Operation = Constants.OPERATION_BROWSE)]
        public ResponseResultModel<IEnumerable<Scf_Scf_ViewModel>> ScfScfSearch(string scfname, string scftype, string year, int pageindex = 1, int pagesize = 10)
        {
            using (_dbContext)
            {


                var response = ResponseModelFactory<IEnumerable<Scf_Scf_ViewModel>>.CreateResultInstance;
                Expression<Func<Scf_Scf, bool>> where = p => true;


                where = where.CheckNullAnd(x => x.ScfName.Contains(scfname), scfname);
                where = where.CheckNullAnd(x => x.ScfType == scftype, scftype);

                where = where.CheckNullAnd(x => x.Year == year, year);
                where = where.And(x => x.Deleted == false);
                var query = _dbContext.SCF_Scf.AsQueryable();

                query = query.Where(where.Expand()).OrderByDescending(x => x.Id);

                var list = query.Paged(pageindex, pagesize).ToList();
                var totalCount = query.Count();
                var data = list.Select(_mapper.Map<Scf_Scf, Scf_Scf_ViewModel>);
                response.SetData(data, totalCount);
                return response;
            }

        }
    }
}
